<?php

namespace App\Models;

use App\Services\GoodsService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PromotionGoods extends Model
{
    use HasFactory;

    protected $table = 'sp_promotion_goods';
    protected $primaryKey = 'id';
    public $timestamps = false;

    const CACHE_LIMIT_NUM = 8;//默认数据条数
    const H5_PROMOTION_GOODS_LIST = 'h5_promotion_goods_list';

    const ACTIVITY_NOT = 1;//不可推广
    const ACTIVITY_YES = 2; //可推广

    //status推广状态：1上架推广  2下架推广
    const PROMOTION_GOODS_STATUS_UP = 1;//上架推广
    const PROMOTION_GOODS_STATUS_DOWN = 2;//下架推广

    //show_type显示类型：1h5  2app
    const PROMOTION_GOODS_SHOW_TYPE_H5 = 1;//h5
    const PROMOTION_GOODS_SHOW_TYPE_APP = 2;//app

    public static function getList($show_type, $promotion_cate_id, $pageSize)
    {
        $list = PromotionGoods::from('sp_promotion_goods as pg')
            ->leftjoin('sp_goods_2 as g', 'g.id', 'pg.goods_id')
            ->select(array('pg.goods_id', 'g.category_id', 'g.title', 'g.imgurl', 'g.new_grade', 'g.rent_label', 'g.is_huabei', 'g.min_price', 'g.min_lease_term'))
            ->where(array(
                'pg.show_type' => $show_type,
                'pg.promotion_cate_id' => $promotion_cate_id,
                'pg.status' => self::PROMOTION_GOODS_STATUS_UP,
                'g.is_delete' => GoodsService::GOODS_NO_DELETE_STATUS,
                'g.status' => GoodsService::GOODS_STATUS_PUT
            ))
            ->orderBy('pg.sort', 'asc')
            ->paginate($pageSize);
        $list = $list ? $list->toArray() : [];
        return $list;
    }

    //商品推广列表
    protected function GetPromotionGoodsList($pageSize, $where)
    {
        $select = array('pg.id', 'pg.sort', 'pg.goods_id', 'g.title', 'g.imgurl', 'c.name as category_name', 'pg.shelves_time', 'su.shopname', 'pg.status');
        $data = $this
            ->from('sp_promotion_goods as pg')
            ->leftjoin('sp_home_promotion_category as hpc', 'hpc.id', 'pg.promotion_cate_id')
            ->leftjoin('sp_goods_2 as g', 'g.id', 'pg.goods_id')
            ->leftjoin('sp_shop_user_2 as su', 'su.member_id', 'pg.member_id')
            ->leftjoin('sp_category as c', 'c.id', 'pg.category_id')
            ->select($select)
            ->where($where)
            ->orderBy('pg.sort', 'asc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    protected function editData($where, $data)
    {
        return $data = $this->where($where)->update($data);
    }


}
